<?php 
session_start();
if (isset($_POST['dt']) && isset($_POST['tiku']) && isset($_SESSION['openid'])){
	$dt=trim($_POST['dt']);
    $tiku=$_POST['tiku'];
	include_once("../c/config.php");
	$mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
	if (!$mysqli->set_charset("utf8")) {printf("Error loading character set utf8: %s\n", $mysqliTwo->error);}

	$getFile='../3c.txt';
	$tiDataInfo=json_decode(file_get_contents($getFile), true); //考题答案
	$tiData=$tiDataInfo['d'];
	if (empty($tiDataInfo)){
		$tiData=array();
		$sql="select id,dan from ti  ";
		$query = $mysqli->query($sql);
		while ($list=$query->fetch_array()){
			$tiData[$list['id']]=array('dan'=>$list['dan']);  
		}
		file_put_contents($getFile, json_encode(array('d'=>$tiData)));
	}else{
		$tiData=$tiDataInfo['d'];
	} 


	$openid=$_SESSION['openid'];
	 
	$dati=json_decode($dt);
	$tikuTmp=json_decode($tiku);
	$dadui=0; //回答正确的提数
	$fenshu=0; //考生分数
	foreach ($dati as $k => $v){
	    if (!empty($v)){
	        if ($tiData[$tikuTmp[$k]]['dan'] == $v){
	            
	            $fenshu++;
	           
	            $dadui++;
	        }
	    }
	}

	$queryUser=$mysqli->query("select name,phone,hao,fen from u3 where openid='".$openid."' limit 0, 1");
	if($queryUser->num_rows>0){
		$userInfo=$queryUser->fetch_array();
	}else{
		exit;
	}
	$mysqli->query("insert into ci3(fen,dadui,name,phone,hao,openid,cishu,dati,tiku,createtime) values(".$fenshu.",".$dadui.",'".$userInfo['name']."','".$userInfo['phone']."', '".$userInfo['hao']."','".$openid."','1','".$dt."','".$tiku."','".date("Y-m-d H:i:s")."') ");
	$mysqli->query("update users set fen=".$fenshu."  where openid='".$openid."' ");
	$mysqli->query("update u3 set fen=".$fenshu.",dadui='".$dadui."', cishu=cishu+1  where openid='".$openid."' ");

	
}
echo json_encode(array('status'=>'ok'));

?>